﻿$(function () {
    // do something 
    myTHead = $('#table-thead-fixed thead tr th');
});

$(window).scroll(function () {
    if ($(window).scrollTop() >= 300) {
        //插入“返回顶部”按钮
        if ($(".back-to-top").size() == 0) {
            $("body").append(
                `<div class="back-to-top" style="position:fixed; bottom: 10px; right: 10px;">
                    <a class="btn btn-default" href="#top">
                        <span class="glyphicon glyphicon-arrow-up"></span>
                    </a>
                </div>`
            )
        }
        //固定住THead
        if ($('#temp-th').size() == 0) {
            $('#table-thead-fixed tbody').prepend(`<tr id="temp-th"></tr>`);
            $('#temp-th').html($('#table-thead-fixed thead tr').html())
        }

        for (var i = 0; i < $('#table-thead-fixed thead tr th').size() ; i++) {
            $('#table-thead-fixed thead tr th').eq(i).width(myTHead.eq(i).width());
        }
        $('#table-thead-fixed thead').css({ 'position': 'fixed', 'top': '0'
    })
        $('#table-thead-fixed thead th').css({
            'background': 'linear-gradient(#fff,#ccc)'
    })

    }
    //释放固定了的THead
    if ($(window).scrollTop() < 300) {
        $('#table-thead-fixed thead').css({ 'position': 'unset', 'top': 'unset' });
        $('#temp-th').remove()
        $(".back-to-top").remove();
    }
});

